package com.wjyf.reducing_bento.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjyf.reducing_bento.annotation.AuthCheck;
import com.wjyf.reducing_bento.common.BaseResponse;
import com.wjyf.reducing_bento.common.ErrorCode;
import com.wjyf.reducing_bento.common.ResultUtils;
import com.wjyf.reducing_bento.model.dto.OrderIndentDto;
import com.wjyf.reducing_bento.service.IndentService;
import com.wjyf.reducing_bento.service.UserService;
import com.wjyf.reducing_bento.model.vo.IndentInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.wjyf.reducing_bento.constant.UserConstant.ADMIN;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author wjyf
 * @since 2023-03-14 08:36:33
 */
@RestController
@RequestMapping("/indent-bean")
@Api(tags = "订单表")
public class IndentController {

    @Resource
    private IndentService indentService;

    @Resource
    private UserService userService;

    @PostMapping("order")
    @ApiOperation("下单")
    public BaseResponse order(@RequestBody OrderIndentDto orderIndentDto, HttpServletRequest request) {
        boolean isSuccess = indentService.order(orderIndentDto, request);
        return ResultUtils.success(isSuccess);
    }

    @GetMapping("get")
    @ApiOperation("管理员获取订单列表分页")
    @AuthCheck(mustRole = ADMIN)
    public BaseResponse get(@RequestParam @ApiParam("当前页号") Long current,
                            @RequestParam @ApiParam("页面大小") Long pageSize,
                            @RequestParam(required = false) @ApiParam("(可传可不传)1表示查询所有的午餐，2表示查询所有的晚餐") Integer flag) {
        Page<IndentInfoVo> page = indentService.getPage(current, pageSize, null, flag);
        return ResultUtils.success(page);
    }

    @ApiOperation("获取当天午餐和晚餐的数量")
    @GetMapping("analyse")
    @AuthCheck(mustRole = ADMIN)
    public BaseResponse analyse() {
        Map<String, Long> resMap = indentService.getAnalyseMap();
        return ResultUtils.success(resMap);
    }

    @GetMapping("getUserOrder")
    @ApiOperation("用户获取订单列表分页")
    public BaseResponse getUserOrder(@RequestParam @ApiParam("当前页号") Long current,
                                     @RequestParam @ApiParam("页面大小") Long pageSize,
                                     HttpServletRequest request) {
        Page<IndentInfoVo> page = indentService.getPage(current, pageSize, userService.getUser(request).getUId(), null);
        return ResultUtils.success(page);
    }


    @ApiOperation("用户取消订单")
    @PostMapping("cancel-indent/{inId}")
    public BaseResponse cancelIndent(@PathVariable Long inId) {
        boolean isSuccess = indentService.cancelIndent(inId);
        return isSuccess ? ResultUtils.success(isSuccess) : ResultUtils.error(ErrorCode.OPERATION_ERROR);
    }

    @ApiOperation("管理员确认订单")
    @PutMapping("confirm-indent/{inId}")
    @AuthCheck(mustRole = ADMIN)
    public BaseResponse confirmIndent(@PathVariable @ApiParam("订单单号") Long inId){
        indentService.confirmIndent(inId);
        return ResultUtils.success(null);
    }
}
